package cn.cau.control;

import cn.cau.dao.AuditingApplyRecordsDao;
import cn.cau.dao.AuditingTopInfoDao;
import cn.cau.entity.*;
import cn.cau.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class CodeAuditingListController {
    /*
    编码审核
     */

    @Autowired
    private AuditingListService auditingListService;

    @Autowired
    private AuditingTopService auditingTopService;

    @Autowired
    private AuditingMidService auditingMidService;

    @Autowired
    private AuditingBotService auditingBotService;

    @Autowired
    private CategoryService categoryService;


    @RequestMapping(value = "/auditing/list/design", method = RequestMethod.GET)     //到待处理审核事项路由
    public ModelAndView designlist() {

        List<AuditingApplyRecord> records = new ArrayList<>();   //总待审核表
        //首先从apply表中获取待审核记录
        List<CodeManageRecord> designApplies = auditingListService.getAllUnDoRecord();
        for(CodeManageRecord apply : designApplies){
            AuditingApplyRecord temp_record = new AuditingApplyRecord();
            int old_id = apply.getOldCodeRuleId(),new_id = apply.getNewCodeRuleId();
            CodeRule rule = new CodeRule();

            int op = apply.getManageOperation();
            if(op!=2){
                continue;
            }
            if(op==0){
                temp_record.setOperation("修改申请");
            }
            else if(op==1){
                temp_record.setOperation("废除申请");
            }
            else{
                temp_record.setOperation("设计申请");
            }

            if(op==1){
                rule = auditingListService.getReluById(old_id);  //如果为废除申请，则对old_rule进行操作
            }
            else{
                rule = auditingListService.getReluById(new_id);  //否则，对new_rule进行操作
            }

            //todo
            //待新增状态、申请操作等信息

            temp_record.setIndex(apply.getCodeRuleManageId());
            temp_record.setReason(apply.getManageContent());
            temp_record.setOp(op);    //0代表修改，1代表设计，2代表废除
            temp_record.setState(rule.getCodeState());
            temp_record.setOldId(apply.getOldCodeRuleId());
            temp_record.setNewId(apply.getNewCodeRuleId());     //newId中存储申请对应的主表id
            temp_record.setDate(apply.getApplyTime());         //日期
            temp_record.setUserId(apply.getManageManId());       //申请人
            temp_record.setObjectName(rule.getCodeRuleName());  //获取名称
            records.add(temp_record);
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("records",records);
        modelAndView.setViewName("/codeAuditing/codeAuditingList");
        return modelAndView;
    }

    @RequestMapping(value = "/auditing/list/modify", method = RequestMethod.GET)     //到待处理审核事项路由
    public ModelAndView modifylist() {

        List<AuditingApplyRecord> records = new ArrayList<>();   //总待审核表
        //首先从apply表中获取待审核记录
        List<CodeManageRecord> designApplies = auditingListService.getAllUnDoRecord();
        for(CodeManageRecord apply : designApplies){
            AuditingApplyRecord temp_record = new AuditingApplyRecord();
            int old_id = apply.getOldCodeRuleId(),new_id = apply.getNewCodeRuleId();
            CodeRule rule = new CodeRule();

            int op = apply.getManageOperation();
            if(op!=0){
                continue;
            }
            if(op==0){
                temp_record.setOperation("修改申请");
            }
            else if(op==1){
                temp_record.setOperation("废除申请");
            }
            else{
                temp_record.setOperation("设计申请");
            }

            if(op==1){
                rule = auditingListService.getReluById(old_id);  //如果为废除申请，则对old_rule进行操作
            }
            else{
                rule = auditingListService.getReluById(new_id);  //否则，对new_rule进行操作
            }

            //todo
            //待新增状态、申请操作等信息

            temp_record.setIndex(apply.getCodeRuleManageId());
            temp_record.setReason(apply.getManageContent());
            temp_record.setOp(op);    //0代表修改，1代表设计，2代表废除
            temp_record.setState(rule.getCodeState());
            temp_record.setOldId(apply.getOldCodeRuleId());
            temp_record.setNewId(apply.getNewCodeRuleId());     //newId中存储申请对应的主表id
            temp_record.setDate(apply.getApplyTime());         //日期
            temp_record.setUserId(apply.getManageManId());       //申请人
            temp_record.setObjectName(rule.getCodeRuleName());  //获取名称
            records.add(temp_record);
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("records",records);
        modelAndView.setViewName("/codeAuditing/codeAuditingList");
        return modelAndView;
    }

    @RequestMapping(value = "/auditing/list/abolish", method = RequestMethod.GET)
    public ModelAndView abolishlist() {

        List<AuditingApplyRecord> records = new ArrayList<>();   //总待审核表
        //首先从apply表中获取待审核记录
        List<CodeManageRecord> designApplies = auditingListService.getAllUnDoRecord();
        for(CodeManageRecord apply : designApplies){
            AuditingApplyRecord temp_record = new AuditingApplyRecord();
            int old_id = apply.getOldCodeRuleId(),new_id = apply.getNewCodeRuleId();
            CodeRule rule = new CodeRule();

            int op = apply.getManageOperation();
            if(op!=1){
                continue;
            }
            if(op==0){
                temp_record.setOperation("修改申请");
            }
            else if(op==1){
                temp_record.setOperation("废除申请");
            }
            else{
                temp_record.setOperation("设计申请");
            }

            if(op==1){
                rule = auditingListService.getReluById(old_id);  //如果为废除申请，则对old_rule进行操作
            }
            else{
                rule = auditingListService.getReluById(new_id);  //否则，对new_rule进行操作
            }

            //todo
            //待新增状态、申请操作等信息

            temp_record.setIndex(apply.getCodeRuleManageId());
            temp_record.setReason(apply.getManageContent());
            temp_record.setOp(op);    //0代表修改，1代表设计，2代表废除
            temp_record.setState(rule.getCodeState());
            temp_record.setOldId(apply.getOldCodeRuleId());
            temp_record.setNewId(apply.getNewCodeRuleId());     //newId中存储申请对应的主表id
            temp_record.setDate(apply.getApplyTime());         //日期
            temp_record.setUserId(apply.getManageManId());       //申请人
            temp_record.setObjectName(rule.getCodeRuleName());  //获取名称
            records.add(temp_record);
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("records",records);
        modelAndView.setViewName("/codeAuditing/codeAuditingList");
        return modelAndView;
    }



}
